/*
 * @(#)ServerResponseJson.java $version 2011. 5. 30.
 *
 */

package org.doit.android.bobple.common;

import junit.framework.Assert;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Choi HongKi
 */
public final class ServerResponseJson {
	private final boolean isSuccess;
	private final JSONObject value;
	private final JSONArray valueArray;

	public ServerResponseJson(JSONObject json) throws JSONException {
		Assert.assertNotNull("JSON Response is null.", json);

		isSuccess = json.getBoolean("success");

		/*
		 * 서버 응답은 JSONObject 또는 JSONArray 두가지 형태로 정해져있음.
		 * 둘 중 어떤 형태로 오는지는 당신이 알겠지... ^____^
		 */
		if (json.get("value") instanceof JSONArray) {
			valueArray = json.getJSONArray("value");
			value = null;
		} else if (json.get("value") instanceof JSONObject) {
			value = json.getJSONObject("value");
			valueArray = null;
		} else {
			throw new RuntimeException("The instane of JSON response 'value' must be JSONObject/JSONArray!");
		}
	}

	public boolean isSuccess() {
		return isSuccess;
	}

	public JSONObject getValue() {
		return value;
	}

	public JSONArray getValues() {
		return valueArray;
	}
}
